package com.example.safeassistan.home.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.base.BaseModel;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.home.model.DeviceDetailModel;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.utile.JsonUtils;
import com.example.safeassistan.widget.WarningDialog;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class DeviceInfoDetail extends BaseActivity implements View.OnClickListener {
    private LinearLayout llUnBind,llRepire;
    private TextView tvLoaction,tvUseDepart,tvliablePerson,tvColor,tvVersion,
            tvDeviceBrand,tvDeviceId,tvDeviceName;
private String deviceId="";//设备id
    private String title="";//标题
    private DeviceDetailModel model;
    public static void actionStart(Context context,String deviceId,String title) {
        Intent intent = new Intent(context, DeviceInfoDetail.class);
        intent.putExtra("deviceId",deviceId);
        intent.putExtra("title",title);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.llUnBind:
                WarningDialog warningDialog=new WarningDialog(getContext(),"确定要解绑该设备吗？",
                        "取消","确定","");
                warningDialog.setClicklistener(new WarningDialog.ClickListenerInterface() {
                    @Override
                    public void doCancel() {
                        warningDialog.dismiss();
                    }

                    @Override
                    public void doConfirm() {
                        warningDialog.dismiss();
                        deviceUntie(deviceId);
                    }
                });
                warningDialog.show();
                break;
         //   case R.id.llRepire:
               // switch (deviceId){
//                    case "0":
           //             DeviceRepairEdit.actionStart(getContext(),deviceId,title);
//                        break;
//                    case "1":
//                        DevicerepairProgress.actionStart(getContext(),1);
//                        break;
//                    case "2":
//                        DevicerepairProgress.actionStart(getContext(),2);
//                        break;
     //           }
          //      break;
        }
    }


    //获取数据
    public void deviceUntie(String deviceId){
        Map map = new HashMap<>();
        map.put("deviceId", deviceId);
        new HttpRequest(DeviceInfoDetail.this).doGet(UrlConstants.taskdeviceUntie, null, map, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success = jsonObject.getBoolean("success");
                    if (success) {
                        Object obj = JsonUtils.fromJson(result.toString(), BaseModel.class);
                        Toast.makeText(DeviceInfoDetail.this,"解绑设备成功！！！",Toast.LENGTH_SHORT).show();
                       finish();
                    }else {
                        Toast.makeText(DeviceInfoDetail.this,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
            }
        });
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.device_info_detail;
    }

    @Override
    protected void initView() {

        deviceId=getIntent().getStringExtra("deviceId");
        title=getIntent().getStringExtra("title");
        setTitleText(title);
        llUnBind=findViewById(R.id.llUnBind);
        tvUseDepart=findViewById(R.id.tvUseDepart);
        tvLoaction=findViewById(R.id.tvLoaction);
        tvDeviceName=findViewById(R.id.tvDeviceName);
        tvColor=findViewById(R.id.tvColor);
        tvliablePerson=findViewById(R.id.tvliablePerson);
        tvVersion=findViewById(R.id.tvVersion);
        tvDeviceId=findViewById(R.id.tvDeviceId);
        tvDeviceBrand=findViewById(R.id.tvDeviceBrand);
        llUnBind.setOnClickListener(this::onClick);
        llRepire=findViewById(R.id.llRepire);
        llRepire.setOnClickListener(this::onClick);
        deviceDetail();
    }

    @Override
    protected void initData() {

    }

    public void deviceDetail(){
        Map map = new HashMap<>();
        map.put("deviceId", deviceId);
        new HttpRequest(this).doGet(UrlConstants.taskqueryDeviceById, null, map, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success = jsonObject.getBoolean("success");
                    if (success) {
                        Object obj = JsonUtils.fromJson(result.toString(), DeviceDetailModel.class);
                        if (obj instanceof DeviceDetailModel){
                             model=(DeviceDetailModel)obj;
                            setView();
                        }
                    }else {
                        Toast.makeText(DeviceInfoDetail.this,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) { }
        });
    }

    public void setView(){
        tvLoaction.setText(model.getResult().getAddress());
        tvUseDepart.setText(model.getResult().getDepartName());
        tvliablePerson.setText(model.getResult().getLiablePerson());
        tvColor.setText(model.getResult().getColor());
        tvVersion.setText(model.getResult().getVersion());
        tvDeviceBrand.setText(model.getResult().getBrand());
        tvDeviceId.setText(model.getResult().getCode());
        tvDeviceName.setText(model.getResult().getName());
    }

    @Override
    protected int getTitleBarType() {
        return 0;
    }
}